// pages/home/chatroom/chatroom.js
const app =getApp()
Component({
  options: {
    addGlobalClass: true,
    have:false
  },

  /**
   * 页面的初始数据
   */
  data: {
    modalName:'',
    inputInit:'',
    bgimg1:app.globalData.bgimg,
    haveImg:app.globalData.haveImg,
    roomId:''
  },
  attached() {
    // var that=this
    this.refresh()
    console.log(app.globalData.userInfo.userInfo.id)
    //得到roomlist从而得到用户数目
    // wx.request({
    //   // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/chat/getChatRoomList',
    //   url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getChatRoomListByUserId',
    //   method: 'POST',
    //   data: {
    //     token: app.globalData.userInfo.token,
    //     userId: app.globalData.userInfo.userInfo.id
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     app.globalData.RoomCount = res.data.list
    //     that.setData({
    //       list: res.data.list,
    //     })
    //   }
    // })
    // //得到userlist 从而得到number
    // wx.request({
    //   // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/user/getFriendsList',
    //   url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getFriendsList',
    //   method: 'POST',
    //   data: {
    //     token: app.globalData.userInfo.token,
    //     userId: app.globalData.userInfo.userInfo.id
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     //除非有改动否则不用重新刷新
    //     app.globalData.FriendCount = res.data.list
    //     that.setData({
    //       users: res.data.list,
    //       bgimg1: app.globalData.bgimg,
    //       haveImg: app.globalData.haveImg
    //     })
    //   }
    // })
   

  },








  methods: {
    // // ListTouch触摸开始
    // ListTouchStart(e) {
    //   this.setData({
    //     ListTouchStart: e.touches[0].pageX
    //   })
    // },
    refresh:function(e)
    {
      var that=this
      //得到roomlist从而得到用户数目
      wx.request({
        // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/chat/getChatRoomList',
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getChatRoomListByUserId',
        method: 'POST',
        data: {
          token: app.globalData.userInfo.token,
          userId: app.globalData.userInfo.userInfo.id
        },
        success: function (res) {
          console.log(res)
          app.globalData.RoomCount = res.data.list
          that.setData({
            list: res.data.list,
          })
        }
      })
      //得到userlist 从而得到number
      wx.request({
        // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/user/getFriendsList',
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getFriendsList',
        method: 'POST',
        data: {
          token: app.globalData.userInfo.token,
          userId: app.globalData.userInfo.userInfo.id
        },
        success: function (res) {
          console.log(res)
          //除非有改动否则不用重新刷新
          app.globalData.FriendCount = res.data.list
          that.setData({
            users: res.data.list,
            bgimg1: app.globalData.bgimg,
            haveImg: app.globalData.haveImg
          })
        }
      })
    },
    goTodetail:function(e)
    {
      console.log(e.currentTarget.dataset)
      // //传参
      // var  info ={
      //   type: e.currentTarget.dataset.type,
      //   content: e.currentTarget.dataset.content
      // }
      var info =e.currentTarget.dataset
      // console.log(info)
      //单独页面单独传参
      wx.navigateTo({
        url: '/pages/home/detail/detail?info=' + JSON.stringify(info),
      })
    },
    searchInput:function(e)
    {
      // console.log(e)
       this.data.roomId = e.detail.value
    },
    search2:function(e)
    {
      //清空
      this.setData({
        inputInit:''
      })
      var that =this
      //查找房间
      // console.log(e)
      var roomID = that.data.roomId
      var token = app.globalData.userInfo.token
      console.log(roomID)
      console.log(token)
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/searchChatRoom',
        method: 'POST',
        data:
        {
          token: token,
          key: roomID
        },
        success: function (res) {
          console.log(res)
          //存在聊天室
          if(res.data.list!=null)
          {
          that.setData({
              modalName:'room',
              room: res.data.list[0]
          }) 
          }else{

          //查找对象
          wx.request({
            url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/searchUser',
            method: 'POST',
            data:
            {
              token: token,
              key: roomID
            },
            success: function (res) {
              console.log(res)
              if (res.data.list[0] != null) {
                that.setData({
                  modalName: 'friend',
                  person: res.data.list[0]
                })
              }
              else {
                that.setData({
                  modalName: 'none',
                  errmsg: "对不起，您搜索的聊天室/用户不存在"
                })
              }
          }
          })
          }
        }

      })
     


    },
    update:function(e)
    {
      this.setData({
        modalName: ''
      }) 
      console.log(e)
      var roomId = e.currentTarget.dataset.id
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/joinChatRoom',
        method: 'POST',
        data:
        {
          token: app.globalData.userInfo.token,
          roomId: roomId * 1
        },
        success:function(e)
        {
          console.log(e)
        }
      })

    },
    update2:function(e)
    {
      this.setData({
        modalName: ''
      })
      console.log(e)
      var userId=e.currentTarget.dataset.id
      var that =this
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/addFriend',
        method: 'POST',
        data:
        {
          token: app.globalData.userInfo.token,
          userId: userId
        },
        success: function (e) {
          console.log(e)
          if(e.data.bool==true)
          {
            that.setData({
              modalName: 'none',
              errmsg: "恭喜你，申请成功"
            })

          }
          else{
          that.setData({
            modalName:'none',
            errmsg:e.data.errmsg
          })
          }
        }
      })

    },
    hideModal:function(e){
      this.setData({
        modalName:''
      })

    },
    add:function(e)
    {
      wx.navigateTo({
        url: '/pages/others/add/add',
      })
    }
  }


})